package org.itboys.seller.service;

import org.apache.commons.collections.CollectionUtils;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseService;
import org.itboys.seller.entity.RankingUserDB;
import org.itboys.seller.entity.RewardRecordDB;
import org.mongodb.morphia.query.Query;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * Created by Administrator on 2016/6/13.
 */
@Service
public class RewardRecordService extends BaseService<RewardRecordDB, Long> {

    @Resource(name="sellerDS")
    private MongoDataSource ds;

    @Override
    protected MongoDataSource getMongoDataSource() {
        return ds;
    }

    @Override
    protected Class<RewardRecordDB> getEntityClass() {
        return RewardRecordDB.class;
    }

    public RewardRecordDB getLastedRecord(int type) {
        List<RewardRecordDB> dbList = ds.createQuery(RewardRecordDB.class)
                .order("-ct").asList();
        for (RewardRecordDB recordDB : dbList) {
            List<Long> uIds;
            if (type == 0) {
                uIds = recordDB.getUserIds();
            } else if (type == 1) {
                uIds = recordDB.getsUserIds();
            } else {
                uIds = recordDB.getSsUserIds();
            }
            if (CollectionUtils.isNotEmpty(uIds)) {
                return recordDB;
            }
        }
        return null;
    }
}
